package org.openschool.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.openschool.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UsersDaoImpl implements UsersDao{

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void addUser(User person) {
		sessionFactory.getCurrentSession().save(person);
	}

	@Override
	public List<User> listUsers() {
		return sessionFactory.getCurrentSession().createQuery("from Users").list();
	}

	@Override
	public void removeUser(String username) {
		User user = (User)sessionFactory.getCurrentSession().load(User.class, username);
		
		if(null!=user){
			sessionFactory.getCurrentSession().delete(user);
		}
		
	}

	@Override
	public User getUser(String username) {
		User user = (User)sessionFactory.getCurrentSession().get(User.class, username);
		return user;
	}

	@Override
	public void updateUser(User user) {
		sessionFactory.getCurrentSession().update(user);
	}
}
